#include<stdio.h>
#include<stdlib.h>
#include "../include/linkedlist.h"

void main()
{

	struct node *start;
	struct node *p, *q;
	int nodedata;
	int nodes;
	int count=1;
	
	FILE *fp;
	fp=fopen("Desktop\\files\\linkedlist.txt", "wb");
	
	printf("How many nodes you wish to insert:  ");
	scanf("%d", &nodes);
	while(count<=nodes)
	{
		printf("Enter the data in node %d: ", count);
		scanf("%d", &nodedata);
		//p=(struct node *)malloc(sizeof(struct node));
		p=getmemory();
		p->data=nodedata;
		p->next=NULL;
		if(count==1){
			start=p;
			q=p;
			fwrite(&start->data, sizeof(int), 1, fp);
		}
		else{
			q->next=p;
			q=p;
			fwrite(&q->data, sizeof(int), 1, fp);
		}
		count++;
				
	}
	fclose(fp);
}


//This function allocates the memory to the new node
l_node *getmemory()
{
	l_node *new_node;
	new_node = (l_node *)malloc(sizeof(l_node));
	
	if(new_node == NULL)	//If system runs out of memory
	{
		printf("Insufficient Memory\n");
		exit(0);
	}
	
	return new_node;
}
